package com.ecfront.service.impl;

import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import com.ecfront.dao.UserDAO;
import com.ecfront.dao.UserGradeDAO;
import com.ecfront.exception.UserException;
import com.ecfront.pojo.User;
import com.ecfront.pojo.UserGrade;
import com.ecfront.service.EmailSender;
import com.ecfront.service.UserService;
import com.ecfront.util.MD5;
import com.ecfront.util.email.MailContext;

public class UserServiceImpl implements UserService {
	//日志文件
	private static Logger log = Logger.getLogger(UserServiceImpl.class.getName());
	
	protected String defaultUserPic;
	protected UserDAO userDAO; 
	protected UserGradeDAO userGradeDAO;
	protected EmailSender emailSender;
	protected MailContext mailContextByRegist;
	
	public void setMailContextByRegist(MailContext mailContextByRegist) {
		this.mailContextByRegist = mailContextByRegist;
	}
	public void setEmailSender(EmailSender emailSender) {
		this.emailSender = emailSender;
	}
	public void setDefaultUserPic(String defaultUserPic) {
		this.defaultUserPic = defaultUserPic;
	}
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	public void setUserGradeDAO(UserGradeDAO userGradeDAO) {
		this.userGradeDAO = userGradeDAO;
	}
	/**
	 * 判断登录是否成功
	 * @param userOrEmail 所需用户名或密码
	 * @param loginPwd 所需登录密码
	 * @param isLock 用户是否上锁
	 * @return 登录成功返回true，失败返回false
	 */
	public boolean findUserByNameOrEmailAndPassAndLock(String userOrEmail,
			String loginPwd, int isLock) {
		return userDAO.findUserByNameOrEmailAndPassAndLock(userOrEmail, loginPwd, isLock);
	}
	/**
	 * 增加一个新用户
	 * @param username 所需用户名
	 * @param email 所需email
	 * @param loginPwd 所需登陆密码
	 * @param payPwd 所需支付密码
	 * @param question 所需问题
	 * @param answer 所需答案
	 * @param address 所需地址
	 */
	public void addUser(String username, String email, String loginPwd,
			String payPwd, String question, String answer) throws UserException {
		try{
			User user = new User();
			user.setName(username);
			user.setEmail(email);
			user.setLoginPwd(new MD5().getMD5ofStr(loginPwd));
			user.setPayPwd(new MD5().getMD5ofStr(payPwd));
			user.setQuestion(question);
			user.setAnswer(answer);
			user.setScore(0);
			user.setMoney(0.0);
			user.setIsLock(0);
			user.setPhonoUrl(defaultUserPic);
			user.setRegDate(new Date());
			
			UserGrade userGrade = userGradeDAO.findById(1);
			user.setUg(userGrade);
		
			mailContextByRegist.setToEmail(email);
			mailContextByRegist.setName(username);
			//发送祝贺信
			emailSender.sandMessage(mailContextByRegist);
			
			userDAO.save(user);
		}catch (Exception e) {
			log.debug(e.getMessage());
			throw new UserException("Add User Exception");
		}
	}
	/**
	 * 判断该用户名是否已经存在
	 * @param name 要判断的用户名
	 * @return true为存在，false为不存在
	 */
	public boolean findUserByName(String name){
		User user = userDAO.findByUname(name);
		if(user!=null){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 判断该Eamil地址是否已经存在
	 * @param name 要判断的Eamil地址
	 * @return true为存在，false为不存在
	 */
	public boolean findUserByEmail(String email){
		User user = userDAO.findByUemail(email);
		if(user!=null){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 修改新密码
	 * @param userId 需修改的用户ID
	 * @param oldPassword 需修改的旧密码
	 * @param newPassword 新的密码
	 * @throws UserException
	 */
	public boolean modifLoginPassword(int userId,String oldPassword, String newPassword)
			throws UserException {
		try{
			User user = userDAO.findById(userId);
			if(user.getLoginPwd().equals(oldPassword)){
				user.setLoginPwd(newPassword);
				userDAO.save(user);
				return true;
			}else{
				return false;
			}
		}catch (Exception e) {
			log.debug(e.getMessage());
			throw new UserException("登录密码更改失败");
		}
	}
	/**
	 * 修改支付密码
	 * @param userId 需修改的用户ID
	 * @param oldPassword 需修改的旧密码
	 * @param newPassword 新的密码
	 * @throws UserException
	 */
	public boolean modifPayPassword(int userId,String oldPassword,String newPassword) 
			throws UserException{
		try{
			User user = userDAO.findById(userId);
			if(user.getPayPwd().equals(oldPassword)){
				user.setPayPwd(newPassword);
				userDAO.save(user);
				return true;
			}else{
				return false;
			}
		}catch (Exception e) {
			log.debug(e.getMessage());
			throw new UserException("支付密码更改失败");
		}
	}
	/**
	 * 根据回答返回当前用户的登录密码
	 * @param userId 需返回密码的用户
	 * @param answer 返回密码所需的答案
	 * @return 返回所需密码
	 */
	public String getLoginPwdByAnswer(int userId, String answer)
			throws UserException {
		String result = null;
		try{
			User user = userDAO.findById(userId);
			if(user.getAnswer().equals(answer)){
				result = "产生的随机数";
			}
		}catch(Exception e){
			log.debug(e.getMessage());
			throw new UserException("登录密码返回失败");
		}
		return result;
	}
	/**
	 * 根据回答返回当前用户的支付密码
	 * @param userId 需返回密码的用户
	 * @param answer 返回密码所需的答案
	 * @return 返回所需密码
	 */	
	public String getPayPwdByAnswer(int userId, String answer)
			throws UserException {
		String result = null;		
		try{
			User user = userDAO.findById(userId);
			if(user.getAnswer().equals(answer)){
				result = "产生的随机数";
			}
		}catch(Exception e){
			log.debug(e.getMessage());
			throw new UserException("支付密码返回失败");
		}	
		return result;
	}
}
